package com.baiytfp.hf.HFDataExchange.action.controller.service;

import com.baiytfp.hf.baiytfp.common.db.HFPager;
import com.baiytfp.hf.baiytfp.framework.base.BaseController;
import com.baiytfp.hf.baiytfp.hfutils.HFDBHelper;
import com.baiytfp.hf.baiytfp.hfutils.HFRequest;
import com.baiytfp.hf.baiytfp.hfutils.HFResponse;
import com.baiytfp.hf.baiytfp.hfutils.HFWeb;
import com.baiytfp.hf.HFDataExchange.action.service.TagsService;
import com.baiytfp.hf.HFDataExchange.common.model.base.TableName;
import com.baiytfp.hf.HFDataExchange.common.model.table.Tags;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/service/tags")
public class TagsSController extends BaseController{

	@Autowired
	public TagsService tagsService;

	public TagsSController() {

	}

	@RequestMapping(value="/tagslist", method= RequestMethod.POST)
	public ModelAndView tagslist(HttpServletRequest request,HttpServletResponse response) throws Exception {
		HFPager hfPager = new HFPager(request);

		Map<String, String> map = new HashMap<String, String>();
		map.put("tag_title", HFRequest.getIstance().getString(request, "tag_title", ""));
		map.put("tag_type", HFRequest.getIstance().getString(request, "tag_type", ""));

		hfPager.setParams(map);

		List<Tags> list = tagsService.getTagsList(hfPager);


		HFResponse.getIstance().returnResult(response, hfPager.getResult());

		return null;
	}

	//编辑
	@RequestMapping(value="/tagsedit", method= RequestMethod.POST)
	public ModelAndView tagsedit(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String key = HFRequest.getIstance().getString(request, "itemid");

		if("".equals(key) || "0".equals(key)){	//新增
			Tags itemObj = HFWeb.getIstance().toBean(request, Tags.class);

			itemObj.setId(HFDBHelper.getIDGenerator());	//生成ID

			long res = HFDBHelper.add(TableName.TAGS, itemObj);

			HFResponse.getIstance().returnResultByRes(response, res);
		}else{	//编辑
			Tags itemObj = HFDBHelper.read(TableName.TAGS, Tags.class, key);
			itemObj = HFWeb.getIstance().toBean(request, itemObj);

			long res = HFDBHelper.update(TableName.TAGS, " where id = ? ", itemObj, new Object[] { itemObj.getId() });
			HFResponse.getIstance().returnResultByRes(response, res);
		}

		return null;
	}

	//删除 (支持多个ID, 如 1, 2, 3 这种写法)
	@RequestMapping("/tagsdel")
	public ModelAndView tagsdel(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String ids = HFRequest.getIstance().getString(request, "itemid");

		HFResponse.getIstance().returnResultByRes(response, HFDBHelper.delete(TableName.TAGS, ids));

		return null;
	}


}
